package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.dao.SysAppMenuDao;
import com.cyt.sys.model.po.SysAppMenu;
import com.cyt.sys.model.po.SysDict;
import com.cyt.sys.model.vo.SysAppMenuVo;
import com.cyt.sys.service.SysAppMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *@Author: dingxu
 *@Date: 2019-04-03 8:19
 *@Description:数据字典类型service实现类
 */
@Service("baseAppMenuService")
public class SysAppMenuServiceImpl extends BaseServiceImpl<SysAppMenu> implements SysAppMenuService {


    @Resource
    private SysAppMenuDao sysAppMenuDao;
    @Autowired
    public void setBaseDao(SysAppMenuDao sysAppMenuDao) {
        this.baseDao = sysAppMenuDao;
    }

    /**
     * 应用菜单分页查询接口
     *
     * @param sysAppMenuVo 对象
     * @param pageSize 第几页
     * @param pageNum 每页数量
     * @return ReturnResult 对象
     * @author dingxu
     * @date 2019-04-02 14:25
     */
    @Override
    public ReturnResult page(SysAppMenuVo sysAppMenuVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        List<SysAppMenu> list= sysAppMenuDao.selectByExample(example);
        PageInfo<SysAppMenu> page=new PageInfo<SysAppMenu>(list);
        Map<String,Object> map=new HashedMap();
        map.put("page",page.getList());
        return new ReturnResult(page.getList(),page.getTotal(),page.isHasNextPage());
    }



    /**
     * 添加修改应用菜单信息
     *
     *@author dingxu
     *@date 2019-04-02 14:25
     *@param sysAppMenuVo 对象
     *@return ReturnResult
     */
    @Override
    public ReturnResult addOrUpdate(SysAppMenuVo sysAppMenuVo) {
        if (sysAppMenuVo.getId() == null || "".equals(sysAppMenuVo.getId())) {
            SysAppMenu sysAppMenu = new SysAppMenu();
            sysAppMenu.setId(BaseUtil.uId());
            sysAppMenu.setAppId(sysAppMenuVo.getAppId());
            sysAppMenu.setMenuId(sysAppMenuVo.getMenuId());
            sysAppMenu.setCreateTime(new Date());
            int id = sysAppMenuDao.insert(sysAppMenu);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            SysAppMenu sysAppMenu = sysAppMenuDao.selectByPrimaryKey(sysAppMenuVo.getId());
            sysAppMenu.setAppId(sysAppMenuVo.getAppId());
            sysAppMenu.setMenuId(sysAppMenuVo.getMenuId());
            sysAppMenu.setCreateTime(new Date());
            int id = sysAppMenuDao.updateByPrimaryKey(sysAppMenu);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }


    /**
     * 删除字典类型
     *
     *@author dingxu
     *@date 2019-04-02 14:25
     *@param ids id数组集字符串
     *@return ReturnResult
     */

    public ReturnResult delete(String ids) {
        String[] idList=ids.split(",");
        boolean flage=true;
        StringBuffer stringBuffer=new StringBuffer();
        for (String id:idList){
            SysAppMenu sysAppMenu = sysAppMenuDao.selectByPrimaryKey(id);
            int num= sysAppMenuDao.delete(sysAppMenu);
            if(num>0){
                flage=true;
            }else{
                stringBuffer.append("");
            }
        }
        if(flage){
            return new ReturnResult();
        }else{
            return new ReturnResult(ErrorCode.CODE_101_MSG);
        }

    }

    @Override
    public List<String> selectMenuIdByAppId(String appId) {
        return sysAppMenuDao.selectMenuIdByAppId(appId);
    }
}